package com.fulinlin;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * redisUtil aop 类 添加日志
 *
 * @author Dong_Jia_Qi on 2022/1/19
 */
@Aspect
@Component
@Slf4j
public class RedisAop {
    @Pointcut("execution (* com.fulinlin.RedisUtil.*(..))")
    void all(){
    }
    @Before("all()")
    void allBefore(JoinPoint joinPoint){
        Object[] objs = joinPoint.getArgs();
        Object target = joinPoint.getTarget();
        Signature sig = joinPoint.getSignature();
        MethodSignature msig = (MethodSignature) sig;
        String name = "获得方法名失败";
        try {
            Method method = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
            name= method.getName();
        } catch (NoSuchMethodException e) {
            log.error(e.getMessage(),e);
        }
        log.info("{} 进入{}方法, 传入参数{}" ,target.getClass().getName(),name ,objs);
    }
}
